﻿/**********************************************************************
/// <summary>
// Name of File     :   ListUsersPresenter.cs
// Author           :   Parminder Singh
// Create date      :   17th Sep 2011
/// </summary>
************************************************************************/

using System.Collections.Generic;
using Crystal.Common.Model;
using CrystalAdmin.BLL;
using System;
using System.Windows.Forms;
using System.IO;
using Crystal.CLL;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinGrid.ExcelExport;
using Infragistics.Documents.Excel;
using System.ComponentModel;

namespace CrystalAdmin.Presenter
{
    public class ListUsersPresenter : IListUsersPresenter
    {
        #region Fields

        private IListsUserView view;
        private CrystalEntities context;
        
        #endregion

        #region Constructors

        public ListUsersPresenter(IListsUserView view)
        {
            this.view = view;
            this.view.FindUserSearchResults += view_GetUsersList;
            this.view.DeleteUser += view_DeleteUser;
            this.view.Searchusers += view_FindUserBySearchResults;
            this.view.ExportToExcel += view_ExportToExcel;
            context = new CrystalEntities(CommonCLL.mstrContextConnectionstring );
        }

        #endregion

        #region Event Handlers

        public void view_FindUserBySearchResults(string searchtext)
        {
            using (UserManager clsUserManager = new UserManager())
            {
                BindingList<USR_Users> UserList = clsUserManager.FindUserByName(context, searchtext);
                this.view.GetUsersList(UserList);
            }
        }
        public void view_GetUsersList()
        {
            using (UserManager clsUserManager = new UserManager())
            {
                USR_Users user = new USR_Users();
                BindingList<USR_Users> UserList = clsUserManager.FindUserList(context);
                this.view.GetUsersList(UserList);
            }
        }

        public void view_DeleteUser()
        {
            using (UserManager clsUserManager = new UserManager())
            {
                clsUserManager.DeleteUser(context, this.view.CurrentUser);
            }
        }

        public void view_ExportToExcel(UltraGrid grdUsers)
        {
            string fileName ="Crystal Users - "+  DateTime.Now.ToString();
            fileName = fileName.Replace(":", "_").Replace("/", "_");
            SaveFileDialog dialog = new SaveFileDialog { FileName = fileName, InitialDirectory = "C:\\", Filter = "Excel (2007-2010 format)(*.xlsx)|*.xlsx|Excel (97-2003 format)(*.xls)|*.xls", DefaultExt = ".xlsx" };
            DialogResult showDialog = dialog.ShowDialog();
            if (showDialog == DialogResult.OK)
            {
                string strExtension = System.IO.Path.GetExtension(dialog.FileName);
                UltraGridExcelExporter ExpToExcel = new UltraGridExcelExporter();
                WorkbookFormat selectedformat;
                if (strExtension == ".xlsx")
                    selectedformat = WorkbookFormat.Excel2007;
                else
                    selectedformat = WorkbookFormat.Excel97To2003;
                Workbook ExcWorkbook = new Workbook(selectedformat, WorkbookPaletteMode.CustomPalette);
                Worksheet lobjws = ExcWorkbook.Worksheets.Add("Crystal Users");
                ExpToExcel.Export(grdUsers, lobjws, 0, 0);
                ExcWorkbook.Save(dialog.FileName);
                DialogResult diaresult = CYMessageBox.Show(MessageManager.GetMessage("EXCELFILECREATED"), "Export", CYMessageBox.CustomButtons.OK, CYMessageBox.CYIcon.Warning);
            }
        }
        #endregion
    }
}
